﻿

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;

using Xc.DataAccess.Core;

namespace Xc.DataAccess.Core.Services
{
    /// <summary>
    /// 转换器
    /// </summary>
    public class ConverterService : IConverterService
    {
        /// <summary>
        /// 转换为字节码数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public ResultData<byte[]> ToBinaryArray<T>(T data)
        {
            try
            {
                var formatter = new BinaryFormatter();
                MemoryStream binaryWriter = new MemoryStream();
                formatter.Serialize(binaryWriter, data);
                return ResultData<byte[]>.Accept("解析成功", binaryWriter.ToArray());

            }
            catch (System.Exception err)
            {
                return ResultData<byte[]>.Reject(err.Message, null);
            }
        }
        /// <summary>
        /// 将字节码数组转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public ResultData<T> FromBinaryArray<T>(byte[] data)
        {
            try
            {
                var formatter = new BinaryFormatter();
                MemoryStream binaryWriter = new MemoryStream();
                binaryWriter.Write(data,0,data.Length);
                var temp = (T)formatter.Deserialize(binaryWriter);
                return ResultData<T>.Accept("转换成功", temp);
            }
            catch (System.Exception err)
            {
                return ResultData<T>.Reject(err.Message, default);
            }
        }
        /// <summary>
        /// 将对象转换为Yaml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public string ToYamlString<T>(T data)
        {
            YamlDotNet.Serialization.Serializer serializer = new YamlDotNet.Serialization.Serializer();
            StringWriter stringWriter = new StringWriter();
            serializer.Serialize(stringWriter, data);
            return stringWriter.ToString();
        }
        /// <summary>
        /// 将Yaml字符串转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="yamlText"></param>
        /// <returns></returns>
        public T FromYamlString<T>(string yamlText)
        {
            StringReader stringReader = new StringReader(yamlText);
            YamlDotNet.Serialization.Deserializer serializer = new YamlDotNet.Serialization.Deserializer();
            return serializer.Deserialize<T>(stringReader);
        }
        /// <summary>
        /// 转换为Json字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public string ToJsonString<T>(T data)
        {
            return System.Text.Json.JsonSerializer.Serialize(data,new System.Text.Json.JsonSerializerOptions 
            {
                WriteIndented=true,
                DefaultIgnoreCondition=System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
 
            });
        }
        /// <summary>
        /// 从Json字符串回转回来
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonText"></param>
        /// <returns></returns>
        public T FromJsonString<T>(string jsonText)
        {
            return System.Text.Json.JsonSerializer.Deserialize<T>(jsonText);
        }
        /// <summary>
        /// 转换为xml 字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public string ToXmlString<T>(T data)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            StringWriter stringWriter = new StringWriter();
            xmlSerializer.Serialize(stringWriter, data);
            return stringWriter.ToString();
        }
        /// <summary>
        /// 从XMl字符串回转回来
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public T FromXmlString<T>(string xml)
        {
            StringReader stringReader = new StringReader(xml);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(stringReader);
        }
    }
}
